1,rest调用第三方接口(第三方发送post请求,json对象)
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
//定义请求参数类型,这里用json所以是MediaType.APPLICATION_JSON
headers.setContentType(MediaType.APPLICATION_JSON);
//如果有参数可以放进map里
HashMap<String, Object> map = new HashMap<String, Object>();
HttpEntity<HashMap<String, Object>> request = new HttpEntity<HashMap<String, Object>>(map,headers);
ResponseEntity<JSONObject> entity = restTemplate.postForEntity(url, request, JSONObject.class);
JSONObject body = entity.getBody();
2,rest调用第三方接口(第三方发送get请求,json串)
String url="http://172.0.0.1:8016/stations.asmx/GetGRGXInfo?pageNumber=1000&pageSize=100";
RestTemplate restTemplate=new RestTemplate();
ResponseEntity<String> jsonObjectResponseEntity = restTemplate.getForEntity(url, String.class);
String body = jsonObjectResponseEntity.getBody();
JSONObject jsonObject = JSONObject.parseObject(body);
3,rest给第三方推送数据(post请求)
RestTemplate restTemplate=new RestTemplate();
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(URL, JSONObject.toJSON(对象), String.class);